package com.kelvin.spiderx.service;

import com.google.gson.Gson;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;

/***
 * @title DrawService
 * @desctption 抽奖服务
 * @author Kelvin
 * @create 2023/9/27 08:23
 **/
@Slf4j
@Service
public class DrawService {

    public List<DrawUser> getDrawUserList(String blogUrl) {
        int page = 1;
        int pageSize = 100;

        RestTemplate restTemplate = new RestTemplate();
        List<DrawUser> drawUserList = new ArrayList<>();

        String blogId = "";
        if( blogUrl.indexOf("?") != -1 ) {
            blogUrl = blogUrl.substring(0 , blogUrl.indexOf("?"));
        }

        if( blogUrl.indexOf("#") != -1 ) {
            blogUrl = blogUrl.substring(0 , blogUrl.indexOf("#"));
        }

        int start = blogUrl.indexOf("details");
        if( start == -1 ) {
            log.info("格式错误");
            return null;
        } else {
            blogId = blogUrl.substring(start+8 , blogUrl.length());
        }

        boolean flag = true;
        while( flag ){
            String url = "https://blog.csdn.net/phoenix/web/v1/comment/list/"+ blogId +"?page="+ page++ +"&size="+ pageSize +"&fold=unfold&commentId=28955159";
            HashMap jsonObject =  restTemplate.getForObject(url, HashMap.class);
            if( !jsonObject.get("code").equals(200) ) {
                return drawUserList;
            }

            if( jsonObject.get("code").equals(200) ) {
                LinkedHashMap blogInfoMap = (LinkedHashMap) jsonObject.get("data");
                List<HashMap> userInfoList = (List<HashMap>) blogInfoMap.get("list");

                for (int i = 0; i < userInfoList.size(); i++) {
                    HashMap userInfo = userInfoList.get(i);
                    HashMap info = (HashMap) userInfo.get("info");
                    drawUserList.add(new DrawUser(info.get("userName")+"" , info.get("nickName")+""));
                }

                if( userInfoList.size() < pageSize ) {
                    log.info("数据加载完毕");
                    return drawUserList;
                }
            }
        }
        log.info("评论人数：" + drawUserList.size());
        return drawUserList;
    }


    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    public static class DrawUser {

        //用户名
        private String userName;

        //昵称
        private String nickName;

    }

    public static void main(String[] args) {

        int page = 1;
        int pageSize = 100;


//        String blogUrl = "https://blog.csdn.net/s445320/article/details/133156575?ddd=11";
        String blogUrl = "https://blog.csdn.net/HHX_01/article/details/133178964";



        String blogId = "";
        if( blogUrl.indexOf("?") != -1 ) {
            blogUrl = blogUrl.substring(0 , blogUrl.indexOf("?"));
        }

        if( blogUrl.indexOf("#") != -1 ) {
            blogUrl = blogUrl.substring(0 , blogUrl.indexOf("#"));
        }

        int start = blogUrl.indexOf("details");
        if( start == -1 ) {
            System.out.println("格式错误");
        } else {
            blogId = blogUrl.substring(start+8 , blogUrl.length());
        }

        System.out.println( blogId );
        RestTemplate restTemplate = new RestTemplate();
        List<DrawUser> drawUserList = new ArrayList<>();

        boolean flag = true;
        while( flag ){
            String url = "https://blog.csdn.net/phoenix/web/v1/comment/list/"+blogId+"?page="+ page++ +"&size="+ pageSize +"&fold=unfold";
            HashMap jsonObject =  restTemplate.getForObject(url, HashMap.class);
            if( !jsonObject.get("code").equals(200) ) {
                flag = false;
                break;
            }

            if( jsonObject.get("code").equals(200) ) {
                LinkedHashMap blogInfoMap = (LinkedHashMap) jsonObject.get("data");
                List<HashMap> userInfoList = (List<HashMap>) blogInfoMap.get("list");

                for (int i = 0; i < userInfoList.size(); i++) {
                    HashMap userInfo = userInfoList.get(i);
                    HashMap info = (HashMap) userInfo.get("info");
                    System.out.println( info.get("userName") + " || " + info.get("nickName") );
                    drawUserList.add(new DrawUser(info.get("userName")+"" , info.get("nickName")+""));
                }

                if( userInfoList.size() < pageSize ) {
                    flag = false;
                    break;
                }
            }
            System.out.println("-----------------------------" +  page );
        }
        System.out.println("-----------------------------");
        System.out.println( drawUserList.size()  );


    }


}
